package com.banciyuan.bcywebview.biz.privatemessage;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.MessageApi;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.layout.SwipeLinearLayout;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.PrivateMessage;

/**
 * Created by squall on 2016/9/12.
 */
public class PrivateMessageListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater inflater;
    private List<PrivateMessage> plist = new ArrayList<>();
    private int type;

    public PrivateMessageListAdapter(List<PrivateMessage> plist, Context mContext, int type) {
        this.mContext = mContext;
        this.plist = plist;
        this.inflater = LayoutInflater.from(mContext);
        this.type = type;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? 0 : 1;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new ViewEmpty(inflater.inflate(R.layout.privatemessage_empty, null));
        } else {
            return new ViewMessage(inflater.inflate(R.layout.private_message_list_item, null));
        }

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(holder.getAdapterPosition()) == 0) {
            ViewEmpty viewEmpty = (ViewEmpty) holder;
            viewEmpty.content_tv.setText(mContext.getString(R.string.no_more_privatemessage));
            if (plist.isEmpty()) {
                viewEmpty.empty.setVisibility(View.VISIBLE);
            } else {
                viewEmpty.empty.setVisibility(View.GONE);
            }
        } else {
            final ViewMessage viewMessage = (ViewMessage) holder;
            final PrivateMessage pm = plist.get(holder.getAdapterPosition() - 1);
            viewMessage.swipeLinearLayout.scrollAuto(SwipeLinearLayout.DIRECTION_SHRINK);
            viewMessage.header.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, pm.getUid());
                }
            });
            ImageLoader.getInstance().displayImage(pm.getAvatar(), viewMessage.header, ImageOptions.optionHead);
            if (!TextUtils.isEmpty(pm.getUname())) {
                viewMessage.name.setText(pm.getUname());
                viewMessage.name.setVisibility(View.VISIBLE);
            } else {
                viewMessage.name.setVisibility(View.GONE);
            }
            viewMessage.time.setText(StringUtil.getOppositeTime(String.valueOf(pm.getCtime())));
            if (!TextUtils.isEmpty(pm.getContent())) {
                viewMessage.content.setText(pm.getContent());
            }
            if (type == 0) {
                if (pm.is_read()) {
                    viewMessage.point.setVisibility(View.GONE);
                } else {
                    viewMessage.point.setVisibility(View.VISIBLE);
                }
            } else {
                viewMessage.point.setVisibility(View.GONE);
            }
            ((View) (viewMessage.header.getParent())).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pm.setIs_read(true);
                    viewMessage.point.setVisibility(android.view.View.GONE);
                    gotoUtil.gotoAct(mContext, PrivateMessageActivity.class, pm);
                }
            });
            viewMessage.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteMessage(pm, viewMessage);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return plist.size() + 1;
    }

    private class ViewEmpty extends RecyclerView.ViewHolder {
        private TextView content_tv;
        private LinearLayout empty;

        public ViewEmpty(View itemView) {
            super(itemView);
            content_tv = (TextView) itemView.findViewById(R.id.tv_group_desc);
            empty = (LinearLayout) itemView.findViewById(R.id.rl_group_empytview);
        }
    }

    private class ViewMessage extends RecyclerView.ViewHolder {
        private ImageView header;
        private TextView name;
        private TextView time;
        private TextView content;
        private TextView point;
        private SwipeLinearLayout swipeLinearLayout;
        private TextView delete;

        public ViewMessage(View view) {
            super(view);
            header = (ImageView) view.findViewById(R.id.message_head_img);
            name = (TextView) view.findViewById(R.id.message_name);
            time = (TextView) view.findViewById(R.id.message_time);
            content = (TextView) view.findViewById(R.id.message_content);
            point = (TextView) view.findViewById(R.id.message_point);
            swipeLinearLayout = (SwipeLinearLayout) view.findViewById(R.id.swipe_ll);
            delete = (TextView) view.findViewById(R.id.delete);
        }
    }

    private void deleteMessage(final PrivateMessage pm, final ViewMessage viewMessage) {
        String url = HttpUtils.BASE_URL + MessageApi.getDeleteMessage();
        List<BcyPair> pairs = new ArrayList<>();
        pairs.add(new BcyPair("message_id", pm.getMessage_id()));
        pairs.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        Map map = HttpUtils.getData(pairs);

        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                        plist.remove(pm);

                        notifyItemRemoved(viewMessage.getLayoutPosition());
                        viewMessage.itemView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                notifyDataSetChanged();
                            }
                        }, 130);
                    } else {
                        viewMessage.swipeLinearLayout.scrollAuto(SwipeLinearLayout.DIRECTION_SHRINK);
                    }
                } catch (Exception e) {
                    MyToast.show(mContext, mContext.getString(R.string.operation_fail));
                    viewMessage.swipeLinearLayout.scrollAuto(SwipeLinearLayout.DIRECTION_SHRINK);
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        };
        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        VolleyQueue.getRquest(mContext).add(requestParam);
    }
}
